package com.suge.home.module.screen.json;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.suge.common.result.message.MessageResult;
import com.suge.message.dao.SmsMessageDAO;
import com.suge.message.dao.dataobject.SmsMessageDO;

public class UpdateGoodsArrievedNotifySetting {



	@Autowired
    private HttpServletRequest request;
	@Autowired
	private SmsMessageDAO smsMessageDAO;
	public void execute(Context context, Navigator nav) {
		String userIdStr = request.getParameter("userId");
		String messageContent = request.getParameter("content");
		String definedMessage = request.getParameter("customerTpls");

		try {
			messageContent = URLDecoder.decode(messageContent, "UTF-8");
			definedMessage = URLDecoder.decode(definedMessage, "UTF-8");

		} catch (UnsupportedEncodingException e) {
		}
		long serviceIdNumber =  3;
		MessageResult messageResult = smsMessageDAO.getSmsMessageTempletesByUserId(123);
		List<SmsMessageDO> messageList = messageResult.getSuccessList();
		boolean result  = false;
		if(messageList!=null && messageList.size()>0){
			for(SmsMessageDO sms: messageList){
				if(sms.getTypeId()==serviceIdNumber){
					result=true;
					sms.setStatus(0);
					sms.setMessageContent(messageContent);
					sms.setDefinedTpl(definedMessage);

					smsMessageDAO.updateSmsTemplete(sms);
					break;
				}
			}
		}
		if(!result){
			SmsMessageDO smsMessageDO = new SmsMessageDO();
			smsMessageDO.setMessageContent(messageContent);
			smsMessageDO.setDefinedTpl(definedMessage);

			smsMessageDO.setTypeId(3);
			smsMessageDO.setUserId(0);
			smsMessageDO.setStatus(0);
			smsMessageDAO.insertSmsTemplete(smsMessageDO);
			result=true;
		}
		
		context.put("isSuccess", result);
		
	
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setSmsMessageDAO(SmsMessageDAO smsMessageDAO) {
		this.smsMessageDAO = smsMessageDAO;
	}

	public SmsMessageDAO getSmsMessageDAO() {
		return smsMessageDAO;
	}




}
